<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<!--在这里写你的css-->
<div th:fragment="stylesheet">

</div>
<!--在这里写你的内容-->

<body id="test" th:fragment="content">

<div  class="panel" style="padding: 10px">
    <div class="panel-heading">
        <h3 class="panel-title">添加新闻</h3>
    </div>
    <form name="add_news">
        <div>
            <div class="box-body col-md-6">
                <label for="newsId">新闻编号</label>
                <input id="newsId" type="text" class="form-control" value="" placeholder="输入新闻编号">
                <br>
                <label for="title">标题</label>
                <input id="title" type="text" class="form-control" placeholder="输入新闻标题">
                <br>
            </div>
            <div class="box-body col-md-6">
                <label for="keyword">关键字</label>
                <input id="keyword" type="text" class="form-control" placeholder="输入关键字">
                <br>
                <label for="picture">选择图片</label>
                <input type="file"  id="picture" class="inputfile">
                <br>
            </div>
            <div class="box-body" style="padding: 10px">
                <label for="content">正文</label>
                <textarea id="content" rows="10" class="form-control" style="resize: none; width: 100%" placeholder="输入正文"></textarea>
                <br>
            </div>
        </div>

        <div  class="box-body ">
            <button id="submit" type="button" class="btn btn-primary btn-block">确定</button>
        </div>

    </form>
</div>
</body>
<!--在这里定义或者引用你的script-->
<div th:fragment="script">
    <script>;

        $('#submit').click(function(){
            if($("#newsId").val()==""){
                alert("标题不能为空")
            }
            else if(/^\d*$/g.test($("#newsId").val())){//判断是否为数字
                $.ajax({
                    type: "post",
                    url: "/contents/news/add",
                    data: {
                        newsId:$("#newsId").val(),
                        keyword:$("#keyword").val(),
                        title:$("#title").val(),
                        content:$("#content").val(),
                        picture:$("#picture").val()
                    },
                    dataType: "json",
                    success: function (data) {
                        alert("successful");
                        $("#test").load(location.href + "#test");//重新加载整个<body>
                    },
                    error:function (data) {
                        alert("error");
                    }

                })
            }else{
                alert("编号必须为数字")
            }
            }
          )
    </script>
</div>

</html>